/*
** Copyright (C) 2011 Viens Yann
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; either
** version 2.1 of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <QInputDialog>
#include <QMessageBox>
#include <ctime>
#include "TabGame.hpp"

TabGame::TabGame(std::string const& tabName, std::string const& tabIcon, std::string const& scoreFile): ATab(tabName, tabIcon), _scoreObj(tabName, scoreFile), _game(NULL)
{
  _global = new QVBoxLayout;
  _global->addWidget(&_cameraDisplay, 0, Qt::AlignHCenter);

  _life = new QLabel("", this);
  _score = new QLabel("Score : 0", this);
  _bestScores = new QPushButton("Meilleurs scores", this);
  connect(_bestScores, SIGNAL(clicked()), this, SLOT(bestScores()));
  _gameAction = new QPushButton("Commencer la partie !", this);
  connect(_gameAction, SIGNAL(clicked()), this, SLOT(gameButtonClicked()));

  _global->addWidget(_life, 0, Qt::AlignHCenter);
  _global->addWidget(_score, 0, Qt::AlignHCenter);
  _global->addWidget(_bestScores, 0, Qt::AlignHCenter);
  _global->addWidget(_gameAction, 0, Qt::AlignHCenter);
  this->setLayout(_global);
}

TabGame::~TabGame()
{
  _scoreObj.writeInFile();
  delete _life;
  delete _score;
  delete _bestScores;
  delete _gameAction;
  delete _global;
  delete _game;
}

void	TabGame::gameButtonClicked()
{
  if (_game->isLaunched() == true)
    {
      stopGame();
    }
  else
    {
      startGame();
    }
}

AGame*	TabGame::getAssociatedGame()
{
	return _game;
}

void	TabGame::bestScores()
{
	_scoreObj.display(this);
}

void	TabGame::startGame()
{
  _game->start();
  _gameAction->setText("Arreter la partie !");
}

void	TabGame::stopGame()
{
  _game->stop();
  _gameAction->setText("Commencer la partie !");
}

IplImage*	TabGame::gameAction(IplImage* frame)
{
  if (_game)
    {
      try
	{
	  frame = _game->gameAction(frame);
	}
      catch (LevelError e)
	{
	  QMessageBox::critical(this, "Erreur de niveau", "Le chargement du niveau a echoue.");
	  stopGame();
	}
    }


  if (_game->isLaunched())
    {
      std::stringstream	converter;
      converter << "Score : " << _game->getScore();
      _score->setText(converter.str().data());
      converter.str("");
      const int	nbLife = _game->getLife();
      if (nbLife <= 0)
	{
	  bool	ok = false;
	  _life->setText("Vous avez perdu.");

	  stopGame();
	  if (_scoreObj.isEmpty() || _scoreObj.getNbScore() < 10 || _game->getScore() > _scoreObj.last().second)
	    {
	      QString		nickName = QInputDialog::getText(this, "Meilleurs scores", "Bravo, tu viens de realiser un excellent score !", QLineEdit::Normal, "Entre ton pseudo", &ok);
	      if (ok)
		_scoreObj.addToScore(nickName.toStdString(), _game->getScore());
	    }
	}
      else
	{
	  if (nbLife == 1)
	    converter << "Vie restante :  " << nbLife;
	  else
	    converter << "Vies restantes :  " << nbLife;
	  _life->setText(converter.str().data());
	}
    }
  return frame;
}
